package com.partaljevic.gambling.beans;

import com.partaljevic.gambling.Identifiable;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.apache.tapestry5.hibernate.annotations.CommitAfter;

@Entity
public class Team implements Identifiable, Serializable {

    @Basic
    private String name;
    @Basic
    private int score;
    @Basic
    private int homeGoals;
    @Basic
    private int awayGoals;
    @Basic
    private int homeWins;
    @Basic
    private int homeDrawn;
    @Basic
    private int homeLosts;
    @Basic
    private int awayWins;
    @Basic
    private int awayDrawn;
    @Basic
    private int awayLosts;
    @Id
    @GeneratedValue
    private Long id;

    public Team() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public int getHomeGoals() {
        return homeGoals;
    }

    public void setHomeGoals(int homeGoals) {
        this.homeGoals = homeGoals;
    }

    public int getAwayGoals() {
        return awayGoals;
    }

    public void setAwayGoals(int awayGoals) {
        this.awayGoals = awayGoals;
    }

    public int getHomeWins() {
        return homeWins;
    }

    public void setHomeWins(int homeWins) {
        this.homeWins = homeWins;
    }

    public int getHomeDrawn() {
        return homeDrawn;
    }

    public void setHomeDrawn(int homeDrawn) {
        this.homeDrawn = homeDrawn;
    }

    public int getHomeLosts() {
        return homeLosts;
    }

    public void setHomeLosts(int homeLosts) {
        this.homeLosts = homeLosts;
    }

    public int getAwayWins() {
        return awayWins;
    }

    public void setAwayWins(int awayWins) {
        this.awayWins = awayWins;
    }

    public int getAwayDrawn() {
        return awayDrawn;
    }

    public void setAwayDrawn(int awayDrawn) {
        this.awayDrawn = awayDrawn;
    }

    public int getAwayLosts() {
        return awayLosts;
    }

    public void setAwayLosts(int awayLosts) {
        this.awayLosts = awayLosts;
    }

    public Team(String name) {
        this.name = name;
    }

    @CommitAfter
    public void processingResult(Result result) {
        String[] downstream = result.getMatchResult().split(":");
        int home = Integer.parseInt(downstream[0]);
        int away = Integer.parseInt(downstream[1]);
        if (name.equals(result.getHome())) {
            this.homeGoals += home;
            if (home > away) {
                this.homeWins++;
                this.score += 3;
            }
            if (home < away) {
                this.homeLosts++;
            }
            if (home == away) {
                this.homeDrawn++;
                this.score += 1;
            }
        } else {
            this.awayGoals += away;
            if (home > away) {
                this.awayLosts++;
            }
            if (home < away) {
                this.awayWins++;
                this.score += 3;
            }
            if (home == away) {
                this.awayDrawn++;
                this.score += 1;
            }
        }
    }

    public String show() {
        return "Name: " + name + " Scores: " + this.score + "   " + this.homeGoals + "  " + this.awayGoals + "  " + this.homeWins + "  " + this.homeDrawn + "  " + this.homeLosts + "  " + this.awayWins + "  " + this.awayDrawn + "  " + this.awayLosts;
    }

    @Override
    public long getID() {
        return id;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}
